<template>
  <!-- <h1 class="title">我的礼物</h1> -->
  <div>
    <van-pull-refresh v-model="formData.refreshing" @refresh="onRefresh">
      <van-list v-model:loading="formData.loading" :finished="formData.finished" finished-text="无更多记录" @load="onload" :immediate-check="false">
        <ul v-if="DataList.length">
          <li v-for="(item,index) in DataList" :key="index" @click="GoOrder(item.id)">
            <van-row class="li">
              <van-col span="9">
                <div class="img-box">
                  <img :src="item.collection.cover" class="img" v-if="VideoIndexOf(item.collection.cover)" />
                  <vueVideo :src="item.collection.cover" class="video" />
                </div>
              </van-col>
              <van-col span="15" class="line">
                <div class="record-cont">
                  <h1>{{item.collection.name}} </h1>
                  <h2>数量：{{item.number?item.number:"1"}}</h2>
                  <h2>赠与人：{{item.userMobile}}</h2>
                  <p>时间：{{item.createTime}}</p>
                </div>
              </van-col>
            </van-row>
            <van-row justify="space-around">
              <van-col>
                <van-button type="primary" class="sed" @click="confirm(2,item.conversionId)">谢谢不需要 </van-button>
              </van-col>
              <val-col>
                <van-button type="primary" class="revice" @click="confirm(1,item.conversionId)">接受礼物 </van-button>
              </val-col>

            </van-row>
          </li>
        </ul>
        <!-- <van-empty description="暂无订单记录" v-else /> -->
      </van-list>
    </van-pull-refresh>

  </div>
  <!-- <van-tabs v-model:active="active" v-for="(item,index) in tabList" :key="index">
    <van-tab :title="item.name"></van-tab>
  </van-tabs> -->

</template>
<script>
import { ref, defineComponent, reactive, onMounted, toRefs, nextTick, computed } from "vue";
import { ToBeReceived, confirmReceive } from "@/api/public";
import { Toast } from "vant";
import { useStore } from "vuex";
import { useRouter } from "vue-router"
import { VideoIndexOf } from "@/utils/phone";
import vueVideo from "@/components/vueVideo.vue";
export default {
  name: "myGift",
  components: { vueVideo },
  setup() {
    const DataList = ref([])
    const store = useStore();
    const router = useRouter()
    const user_info = computed(() => {
      return store.getters.user_info
    })
    const formData = reactive({
      tab: 0,
      page: 1,
      size: 10,
      active: 0,
      total: 0,
      type: null,
      loading: true,
      finished: false,
      isActive: true,
      hasError: false

    })
    const getTab = async (id) => {
      formData.tab = id
    }
    const confirm = async (val, id) => {
      const data = {
        conversionId: id,
        state: val
      }
      const res = await confirmReceive(data)
      console.log(res)

      if (res.data.success) {
        Toast(res.data.message);
        DataList.value = []
        formData.page = 0
        onload()
      } else {
        Toast(res.data.message);
      }

    }
    const GoOrder = (id) => {
      router.push({
        path: '/Mygift',
        // /details
        query: {
          id: id
        }
      })
    }
    const onRefresh = () => {
      DataList.value = []
      formData.page = 1
      formData.total = 0
      formData.loading = false
      formData.finished = false
      formData.refreshing = true
      Toast('刷新成功');

      onload()

    }
    const onClickTab = (index) => {

      console.log(index)
      // formData.finished = true,
      //   formData.loading = false
      formData.page = 1
      DataList.value = []
      let num = index - 1
      if (num < 0) {
        formData.type = null
      } else {
        formData.type = num
      }
      onload()

    }
    onMounted(() => {
      onload()
    })
    const onload = async () => {

      formData.finished = false
      formData.loading = true
      console.log(formData.finished, formData.loading)
      const data = {
        pageNum: formData.page,
        pageSize: formData.size,
        // state: formData.type,
      }
      const res = await ToBeReceived(data)
      if (res.data.data.records.length == 0) {
        router.back()
      }
      console.log(res.data.data.records, "mm")
      formData.loading = false
      formData.total = res.data.data.total
      DataList.value = DataList.value.concat(res.data.data.records)
      formData.page++
      if (formData.finished) {
        formData.loading = true
      }
      if (formData.refreshing) {
        formData.refreshing = false
      }
      console.log(formData.total, DataList.value.length)
      if (res.data.data.records.length == 0) {
        formData.page = 1
        formData.loading = false
        formData.finished = true;
      } else {
        if (formData.total == DataList.value.length) {
          console.log("不加载")
          formData.finished = true;
          formData.loading = false
          return false
        } else {
          formData.finished = false;
          console.log("加载")

        }
      }


    }
    const tabList = reactive([
      {
        id: 0,
        name: '全部'
      },
      {
        id: 0,
        name: '我的赠与'
      },
      {
        id: 0,
        name: '收到赠与'
      },
    ])
    return {
      VideoIndexOf,
      confirm,
      onload,
      tabList,
      formData,
      onRefresh,
      DataList,
      onClickTab,
      GoOrder,
      getTab
    }
  }

}
</script>

<style lang="less" scoped>
.title {
  color: #fff;
  font-size: 34px;
  text-align: center;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 0px;
  font-weight: normal;
  padding-top: 10px;
}
.sed {
  width: 300px;
  height: 68px;
  background: #7d8184;
  border-radius: 34px;
  border: none;
  margin-top: 20px;
}
.revice {
  width: 300px;
  height: 68px;
  background: linear-gradient(-90deg, #37dc6d, #34d1f9);
  border-radius: 34px;
  border: none;
  margin-top: 20px;
}
&:deep(.van-tabs__wrap) {
  background: transparent;
}
&:deep(.van-tabs__nav) {
  background: transparent !important;
  margin: 0px 20px;
}
&:deep(.van-tab) {
  font-size: 31px;
  font-family: Source Han Sans CN;
  font-weight: 400;
  color: #ffffff;
  opacity: 0.8;
}
&:deep(.van-tab--active) {
  .van-tab__text {
    font-size: 36px;
    font-family: Source Han Sans CN;
    font-weight: bold;
    line-height: 30px;
    background: linear-gradient(-90deg, #37dc6d 0%, #34d1f9 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
  }
}
&:deep(.van-tabs__line) {
  background: url('../../../assets/image/tab-active.png');
  background-size: 100% 100%;
  bottom: 40px !important;
}
ul {
  margin: 0px 20px;
  li {
    background: #282a2c;
    border-radius: 20px;
    margin-top: 30px;
    padding: 20px 20px;
    .img-box {
      width: 200px;
      height: 200px;
      background: linear-gradient(0deg, #161718, #4b4c4d);
      border: 2px solid #ffffff;
      border-radius: 20px;
      overflow: hidden;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .record-cont {
      h1 {
        height: 30px;
        font-size: 32px;
        font-family: Source Han Sans CN;
        font-weight: 400;
        color: #ffffff;
        line-height: 53px;
        margin: 0px;
      }
      h2 {
        height: 25px;
        font-size: 26px;
        font-family: Source Han Sans CN;
        font-weight: 400;
        color: #b9c4c7;
        line-height: 48px;
        margin-top: 25px;
      }
      p {
        height: 20px;
        font-size: 26px;
        font-family: Source Han Sans CN;
        font-weight: 300;
        color: #b9c4c7;
        line-height: 48px;
      }
    }
    .space-between {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      align-items: flex-end;
      span {
        font-size: 32px;
        font-family: Source Han Sans CN;
        font-weight: 400;
        color: #ffffff;
        line-height: 73px;

        background: linear-gradient(-90deg, #37dc6d 0%, #34d1f9 100%);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
      }
      p {
        font-size: 40px;
        font-family: Source Han Sans CN;
        font-weight: bold;
        color: #ffffff;
        line-height: 73px;

        background: linear-gradient(-90deg, #37dc6d 0%, #34d1f9 100%);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
      }
    }
  }
}
.title {
  .tabactive {
    font-size: 36px !important;
    font-weight: bold !important;
    color: #ffffff !important;
  }
  padding-left: 20px;
  height: 80px;
  background: #222;
  display: flex;
  align-items: center;
  &:deep(.icon) {
    color: #ffffff;
    font-size: 50px;
  }
  .tab {
    padding-left: 40px;
    span {
      font-size: 36px;
      font-family: Source Han Sans CN;
      font-weight: 400;
      color: #b9c4c7;
      display: inline-block;
      margin-right: 80px;
    }
  }
}
</style>